package com.net.wanjian.networkhospitalmanager.model.taggroup;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Description:
 * Created by lidongcai on 17/5/27.
 * Email:  lidongcaiocc@163.com
 */

public class TagsManager {
    private static TagsManager INSTANCE;

    private DatabaseHelper mDbHelper;

    private TagsManager(Context context) {
        mDbHelper = new DatabaseHelper(context);
    }

    public static TagsManager getInstance(Context context) {
        if (INSTANCE == null) {
            INSTANCE = new TagsManager(context);
        }
        return INSTANCE;
    }

    public String[] getTags() {
        List<String> tagList = new ArrayList<>();
        SQLiteDatabase db = mDbHelper.getReadableDatabase();
        Cursor c = db.query(TagsTable.TABLE_NAME,
                new String[]{TagsTable.TAG}, null, null, null, null, null);
        while (c.moveToNext()) {
            String tag = c.getString(c.getColumnIndex(TagsTable.TAG));
            tagList.add(tag);
        }
        c.close();
        db.close();

        //只显示最近的10个标签
        int size = tagList.size();
        List<String> list = new ArrayList<>();

            if (size >= 20) {
                for (int i = 0; i < 10; i++) {
                    tagList.remove(i);
                }
                size = tagList.size();
            }
        if (size > 10) {
            for (int i = size - 1; i >= size - 10; i--) {
                list.add(tagList.get(i));
            }


            //去掉重复的标签，并保持顺序
            Set set = new HashSet();
            List newList = new ArrayList();
            for (Iterator iter = list.iterator(); iter.hasNext();) {
                Object element = iter.next();
                if (set.add(element))
                    newList.add(element);
            }
            list.clear();
            list.addAll(newList);

            return list.toArray(new String[]{});
        }else {

            Set set = new HashSet();
            List newList = new ArrayList();
            for (Iterator iter = tagList.iterator(); iter.hasNext();) {
                Object element = iter.next();
                if (set.add(element))
                    newList.add(element);
            }
            tagList.clear();
            tagList.addAll(newList);
            return tagList.toArray(new String[]{});
        }
    }

    public void updateTags(CharSequence... tags) {
        clearTags();
        for (CharSequence tag : tags) {
            addTag(tag);
        }
    }

    public void addTag(CharSequence tag) {

        ContentValues values = new ContentValues();
        SQLiteDatabase db = mDbHelper.getWritableDatabase();
        values.put(TagsTable.TAG, tag.toString());
        db.insert(TagsTable.TABLE_NAME, null, values);
        db.close();
    }

    public void clearTags() {
        SQLiteDatabase db = mDbHelper.getWritableDatabase();
        db.delete(TagsTable.TABLE_NAME, null, null);
        db.close();
    }
}
